package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.riddle.lib.model.RiddleConst;
import com.stark.riddle.lib.model.RiddlePrefUtil;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelLevelAdapter;
import flc.ast.databinding.ActivitySelLevelBinding;
import flc.ast.utils.Content;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.SPUtil;

public class SelLevelActivity extends BaseAc<ActivitySelLevelBinding> {

    public static int kind = 0;//0见缝插针
    public static String difficulty;
    public static int max;
    private List<Integer> listShow = new ArrayList<>();//展示的数据
    private int size = 0;//关卡总数
    private int maxPage = 0;//最大页数
    private int page = 0;//当前页数
    private SelLevelAdapter levelAdapter;
    private int oldLevel;
    private String levelName;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_level;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelLevelBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSelLevelLast.setOnClickListener(this);
        mDataBinding.tvSelLevelNext.setOnClickListener(this);
        mDataBinding.rvLevelList.setLayoutManager(new GridLayoutManager(mContext, 4));
        levelAdapter = new SelLevelAdapter();
        mDataBinding.rvLevelList.setAdapter(levelAdapter);
        levelAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSelLevelLast://上一页
                if (page == 0) return;
                page--;
                setData();
                break;
            case R.id.tvSelLevelNext://下一页
                if (page == (maxPage - 1)) return;
                page++;
                setData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (levelAdapter.getItem(position) > (oldLevel - 1)) return;
        switch (kind) {
            case 0://见缝插针
                Intent intent = new Intent();
                intent.putExtra(Content.level, position + (24 * page));
                setResult(RESULT_OK, intent);
                finish();
                break;
            case 1://脑筋急转弯
                TwisterActivity.selLevel = position;
                TwisterActivity.kindName = levelName;
                startActivity(TwisterActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getLevelData();
    }

    //获取关卡相关数据
    private void getLevelData() {
        switch (kind) {
            case 0://见缝插针
                size = max;
                int num = max / 24;
                if (max % 24 > 0) {
                    maxPage = num + 1;
                } else {
                    maxPage = num;
                }
                levelName = Content.NEEDLE_LEVEL;
                oldLevel = SPUtil.getInt(mContext, Content.NEEDLE_LEVEL, 1);//获取对应难度的关卡进度
                setData();
                break;
            case 1://脑筋急转弯
                RiddleDaoHelperManager.getTwisterDbHelper().getCount(difficulty, new IRetCallback<Integer>() {
                    @Override
                    public void onResult(Integer result) {
                        size = result;
                        int num1 = size / 24;
                        if (size % 24 > 0) {
                            maxPage = num1 + 1;
                        } else {
                            maxPage = num1;
                        }
                        levelName = difficulty;
                        oldLevel = RiddlePrefUtil.getUnlockCheckPoint(RiddleConst.FuncType.TWISTER, difficulty);
                        setData();
                    }
                });
                break;
        }
    }

    //设置数据 一次30条
    private void setData() {
        listShow.clear();
        int count = 24 * page;//计算分页
        for (int i = 0; i < 24; i++) {
            if (i + count < size) {
                listShow.add(i + count);
            }
        }
        levelAdapter.setList(listShow);
        //根据页数给关卡分页
        levelAdapter.setPage(page);
        //根据关卡名称获取过关数量
        levelAdapter.setLevelName(levelName);
        levelAdapter.notifyDataSetChanged();
    }

}